using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
/// <summary>
/// Summary description for NhaTuyenDungCommon
/// </summary>
public class NhaTuyenDungCommon
{
    private Common comm;
	public NhaTuyenDungCommon()
	{
        comm = new Common();
		//
		// TODO: Add constructor logic here
		//
	}
    public static string getNganhKDCha(string ma_nganh_kd){
        
        Common comm = new Common();
            object res1 = "0";
            string sqlComm = " select ma_nganh_kd_cha from DanhMuc_nganh_kinh_doanh where ma_nganh_kd = '"+ma_nganh_kd+"' ";
            res1 = comm.getDataTable( sqlComm).Rows[0][0];
            if (res1 == DBNull.Value) res1 = "NULL";
        return res1.ToString();
    }
    public static void insertNTD(objDoanhNghiep objNTD) {
        Common comm = new Common();
        string sqlComm = " INSERT INTO [DoanhNghiep_doanh_nghiep]("+
            "[ma_doanh_nghiep], [tai_khoan], [mat_khau], [ten_doanh_nghiep], [ten_tat], [hinh_anh], [ma_so_huu], [ma_dia_chi], [dia_chi_chi_tiet], [website], [nam_thanh_lap], [ma_quoc_gia], [nguoi_lien_he], [chuc_vu], [dien_thoai], [fax], [email], [ma_so_thue], [so_lao_dong], [ghi_chu], [ma_khu_cn],[nick_yahoo],[nick_skype])" +
            " VALUES(N'" + objNTD.ma_doanh_nghiep + "', '" + objNTD.tai_khoan + "', '" + objNTD.matkhau + "', N'" + objNTD.ten_doanh_nghiep + "', N'" + objNTD.ten_tat + "', N'" + objNTD.hinhanh + "', '" + objNTD.ma_so_huu + "', '" + objNTD.ma_dia_chi + "', N'" + objNTD.dia_chi_chitiet + "', N'" + objNTD.website + "', '" + objNTD.nam_thanh_lap + "', '" + objNTD.ma_quoc_gia + "', N'" + objNTD.nguoi_lien_he + "', N'" + objNTD.chuc_vu + "', '" + objNTD.dien_thoai + "', '" + objNTD.fax + "', '" + objNTD.email + "', '" + objNTD.ma_so_thue + "', " + objNTD.so_lao_dong + ", '', '" + objNTD.ma_khu_cn + "', '" + objNTD.yahoo + "', '" + objNTD.skype + "') ";
        comm.execNoneQuery(sqlComm);
        //string NTD_Id = comm.getLastestId(sqlComm);
        string NTD_Id = objNTD.ma_doanh_nghiep;
        if(NTD_Id.Length > 0){
            foreach (ListItem _li in objNTD.nganh_kd)
	            {
                    sqlComm = " insert into DoanhNghiep_doanh_nghiep_kinh_doanh(ma_doanh_nghiep,ma_nganh_kd,mo_ta) values('" + NTD_Id + "','" + _li.Value + "',N'" + _li.Text + "')";
	                 comm.execNoneQuery(sqlComm);
            }
        }
    }
    public static string getDiaChiTHXString(string ma_dia_chi) {
        string res = "";
        bool isRoot = false;
        string sqlComm ="";
        DataTable dtt;
        Common comm = new Common();
        while (!isRoot) {
            
            sqlComm = "Select Ma_dia_chi, ten_dia_chi, muc, ma_dia_chi_cha from DanhMuc_dia_chi where ma_dia_chi ='" + ma_dia_chi + "'";
            dtt = comm.getDataTable(sqlComm);

            if (dtt.Rows.Count > 0)
            {
                res = dtt.Rows[0]["ten_dia_chi"].ToString() + ", " + res;
                if (Convert.ToInt32(dtt.Rows[0]["muc"]) == 0)
                {
                    isRoot = true;
                }
                else
                {
                    ma_dia_chi = dtt.Rows[0]["ma_dia_chi_cha"].ToString();
                }
            }
            else
            {
                isRoot = true;
            }
            
        }
        return res;
    }

    public static string getTenSoHuu(string ma_so_huu) {
        Common comm = new Common();
        object res1 = "";
        string sqlComm = " select ten_so_Huu from DanhMuc_so_huu where ma_so_huu = '" + ma_so_huu + "' ";
        res1 = comm.getDataTable(sqlComm).Rows[0][0];
        if (res1 == DBNull.Value) res1 = "NULL";
        return res1.ToString();
    }
    public static string getKhuCNString(string ma_khu_cn)
    {
        Common comm = new Common();
        object res1 = "";
        string sqlComm = " select ten_khu_cn from DanhMuc_khu_cong_nghiep where ma_khu_cn = '" + ma_khu_cn + "' ";
        res1 = comm.getDataTable(sqlComm).Rows[0][0];
        if (res1 == DBNull.Value) res1 = "NULL";
        return res1.ToString();
    }
    public static string getQuocGiaString(string ma_quoc_gia)
    {
        Common comm = new Common();
        object res1 = "";
        string sqlComm = " select ten_quoc_gia from DanhMuc_quoc_gia where ma_quoc_gia = '" + ma_quoc_gia + "' ";
        res1 = comm.getDataTable(sqlComm).Rows[0][0];
        if (res1 == DBNull.Value) res1 = "NULL";
        return res1.ToString();
    }
    public  bool login(string taikhoan, string matkhau) {
        string Sql = "Select * from DoanhNghiep_doanh_nghiep where tai_khoan = N'" + taikhoan.Trim().Replace("'", "''") + "' and  mat_khau = N'" + MaHoa.EncryptData(matkhau) + "'";
        if (numOfRecord(Sql) == 1) return true;
        else
            return false;
    }
    public int numOfRecord(string SqlString)
    {
        return comm.getDataTable(SqlString).Rows.Count;
    }
    public objDoanhNghiep getNTD(string tai_khoan) {
        objDoanhNghiep objNTD = new objDoanhNghiep();
            
            string sqlString = "select * from doanhnghiep_doanh_nghiep where tai_khoan = '" + comm.TextFormat(tai_khoan) + "' ";
            DataRow userdtr = comm.getDataTable(sqlString).Rows[0];

            objNTD.chuc_vu = userdtr["chuc_vu"].ToString();

            objNTD.dia_chi_chitiet = userdtr["dia_chi_chi_tiet"].ToString();
            objNTD.dien_thoai = userdtr["dien_thoai"].ToString();
            objNTD.email = userdtr["email"].ToString();
            objNTD.fax = userdtr["fax"].ToString();
            objNTD.ghi_chu = userdtr["ghi_chu"].ToString();
            objNTD.hinhanh = userdtr["hinh_anh"].ToString();
            objNTD.ma_dia_chi = userdtr["ma_dia_chi"].ToString();
            objNTD.ma_doanh_nghiep = userdtr["ma_doanh_nghiep"].ToString();
            objNTD.ma_khu_cn = userdtr["ma_khu_cn"].ToString();
            objNTD.ma_quoc_gia = userdtr["ma_quoc_gia"].ToString();
            objNTD.ma_so_huu = userdtr["ma_so_huu"].ToString();
            objNTD.ma_so_thue = userdtr["ma_so_thue"].ToString();
            objNTD.matkhau = userdtr["mat_khau"].ToString();
            objNTD.nam_thanh_lap = userdtr["nam_thanh_lap"].ToString();
            objNTD.nguoi_lien_he = userdtr["nguoi_lien_he"].ToString();
            objNTD.skype = userdtr["nick_skype"].ToString();
            objNTD.yahoo = userdtr["nick_yahoo"].ToString();
            objNTD.so_lao_dong = int.Parse( userdtr["so_lao_dong"].ToString());
            objNTD.tai_khoan = userdtr["tai_khoan"].ToString();
            objNTD.ten_doanh_nghiep = userdtr["ten_doanh_nghiep"].ToString();
            objNTD.ten_tat = userdtr["ten_tat"].ToString();
            objNTD.website = userdtr["website"].ToString();


            objNTD.nganh_kd = getListNganhKD(objNTD.ma_doanh_nghiep.ToString());

           

        return objNTD;
    }
    public ListItemCollection getListNganhKD(string ma_doanh_nghiep) {
        ListItemCollection li = new ListItemCollection();
        DataTable _temp;
        ListItem _li;
        string sqlString = "Select * from DoanhNghiep_doanh_nghiep_kinh_doanh where ma_doanh_nghiep = '" + ma_doanh_nghiep + "'";
        _temp = comm.getDataTable(sqlString);
        for (int i= 0; i < _temp.Rows.Count; i++)
        {
            _li = new ListItem(_temp.Rows[i]["mo_ta"].ToString(), _temp.Rows[i]["ma_nganh_kd"].ToString());
            li.Add(_li);
        }
        return li;
    }

    public void updateNTD(objDoanhNghiep objNTD) {
        Common comm = new Common();

        string sqlComm = " UPDATE DoanhNghiep_doanh_nghiep" +
            " SET   [ten_doanh_nghiep]= N'" + objNTD.ten_doanh_nghiep + "'," +
            "        [ten_tat]=N'" + objNTD.ten_tat + "'," +
            "        [hinh_anh]= N'" + objNTD.hinhanh + "', " +
            "        [ma_so_huu]='" + objNTD.ma_so_huu + "'," +
            "        [ma_dia_chi]='" + objNTD.ma_dia_chi + "'," +
            "        [dia_chi_chi_tiet]= N'" + objNTD.dia_chi_chitiet + "', " +
            "        [website]= N'" + objNTD.website + "'," +
            "        [nam_thanh_lap]='" + objNTD.nam_thanh_lap + "'," +
            "        [ma_quoc_gia]= '" + objNTD.ma_quoc_gia + "'," +
            "        [nguoi_lien_he]=N'" + objNTD.nguoi_lien_he + "'," +
            "        [chuc_vu]=N'" + objNTD.chuc_vu + "'," +
            "        [dien_thoai]='" + objNTD.dien_thoai + "'," +
            "        [fax]='" + objNTD.fax + "'," +
            "        [email]='" + objNTD.email + "'," +
            "        [ma_so_thue]= '" + objNTD.ma_so_thue + "', " +
            "        [so_lao_dong]=" + objNTD.so_lao_dong + "," +
            "        [ghi_chu]=N'" + objNTD.ghi_chu + "'," +
            "        [ma_khu_cn]='" + objNTD.ma_khu_cn + "'," +
            "        [nick_yahoo]='" + objNTD.yahoo + "'," +
            "        [nick_skype]='" + objNTD.skype + "'" +
            " WHERE [ma_doanh_nghiep] = '" + objNTD.ma_doanh_nghiep + "'";

        if (comm.execNoneQuery(sqlComm) > 0) {
            
            string NTD_Id  = objNTD.ma_doanh_nghiep.ToString();
            sqlComm = "Delete from DoanhNghiep_doanh_nghiep_kinh_doanh where ma_doanh_nghiep = '"+NTD_Id + "'";
            comm.execNoneQuery(sqlComm);
            
            if (NTD_Id.Length > 0)
            {
                foreach (ListItem _li in objNTD.nganh_kd)
                {
                    sqlComm = " insert into DoanhNghiep_doanh_nghiep_kinh_doanh(ma_doanh_nghiep,ma_nganh_kd,mo_ta) values('" + NTD_Id + "','" + _li.Value + "',N'" + _li.Text + "')";
                    comm.execNoneQuery(sqlComm);
                }
            }
        }
    }

    public static string getTenDoanhNghiep(string ma_doanh_nghiep)
    {
        Common common = new Common();
        object obj2 = "";
        string sqlString = " select ten_doanh_nghiep from DoanhNghiep_doanh_nghiep where ma_doanh_nghiep = '" + ma_doanh_nghiep + "' ";
        obj2 = common.getDataTable(sqlString).Rows[0][0];
        if (obj2 == DBNull.Value)
        {
            obj2 = "NULL";
        }
        return obj2.ToString();
    }

    public static bool loginSan(string taikhoan, string pass, string masan)
    {
        bool flag;
        Common common = new Common();
        string sqlString = "select * from DoanhNghiep_doanh_nghiep where ma_doanh_nghiep in (  select ma_doanh_nghiep from SanGiaoDich_doanh_nghiep where ma_san = '" + masan + "' )  and kich_hoat = 1 and tai_khoan = N'" + taikhoan.Trim().Replace("'", "''") + "' and  mat_khau = N'" + MaHoa.EncryptData(pass) + "'";
        try
        {
            if (common.numOfRecord(sqlString) == 1)
            {
                return true;
            }
            flag = false;
        }
        catch (Exception)
        {
            throw;
        }
        return flag;
    }

    public string getMaNTD(string tai_khoan)
    {
        string str2;
        try
        {
            string sqlString = "select * from doanhnghiep_doanh_nghiep where tai_khoan = '" + this.comm.TextFormat(tai_khoan) + "' ";
            DataRow row = this.comm.getDataTable(sqlString).Rows[0];
            str2 = row["ma_doanh_nghiep"].ToString();
        }
        catch (Exception)
        {
            throw;
        }
        return str2;
    }


}
